package com.whiteape.book.ui.base;


import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentPagerAdapter;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.whiteape.book.R;

import java.util.List;

import butterknife.BindView;
import butterknife.Unbinder;

/**
 * Created by newbiechen on 17-4-24.
 */

public abstract class BaseTabFragment extends BaseFragment {
    /**************View***************/
    @BindView(R.id.tab_tl_indicator)
    protected TabLayout mTlIndicator;
    @BindView(R.id.tab_vp)
    protected ViewPager mVp;
    /**************Adapter***************/
    protected TabFragmentPageAdapter tabFragmentPageAdapter;
    /************Params*******************/
    protected List<Fragment> mFragmentList;
    protected List<String> mTitleList;
    private FragmentManager fragmentManager;

    private Unbinder unbinder;

    /**************abstract***********/
    protected abstract List<Fragment> createTabFragments();

    protected abstract List<String> createTabTitles();

    @Override
    protected void initView(Bundle savedInstanceState) {
        super.initView(savedInstanceState);
        setUpTabLayout();
    }


    /*****************rewrite method***************************/


    private void setUpTabLayout() {
        mFragmentList = createTabFragments();
        mTitleList = createTabTitles();

        checkParamsIsRight();

        fragmentManager = getChildFragmentManager();
        tabFragmentPageAdapter = new TabFragmentPageAdapter(fragmentManager);
        mVp.setAdapter(tabFragmentPageAdapter);
        mVp.setOffscreenPageLimit(3);
        mTlIndicator.setupWithViewPager(mVp);
    }

    protected void resetViewPager(){
//        mVp.removeAllViews();
//        setUpTabLayout();
        mFragmentList = createTabFragments();
        mTitleList = createTabTitles();
        tabFragmentPageAdapter.reverse();

    }

    public Fragment getCurrentFragment() {
        return mFragmentList.get(mVp.getCurrentItem());
    }

    /**
     * 检查输入的参数是否正确。即Fragment和title是成对的。
     */
    private void checkParamsIsRight() {
        if (mFragmentList == null || mTitleList == null) {
            throw new IllegalArgumentException("fragmentList or titleList doesn't have null");
        }

        if (mFragmentList.size() != mTitleList.size())
            throw new IllegalArgumentException("fragment and title size must equal");
    }

    /******************inner class*****************/
    public class TabFragmentPageAdapter extends FragmentStatePagerAdapter {

        TabFragmentPageAdapter(FragmentManager fm) {
            super(fm);
        }
        @Override
        public int getItemPosition(Object object) {
            //返回NONE是让系统重新加载
            return POSITION_NONE;
        }

        public void reverse() {

//            Fragment fragment = mFragmentList.get(0);
//            mFragmentList.set(0, mFragmentList.get(1));
//            mFragmentList.set(1, fragment);
//
//            String channel = mTitleList.get(0);
//            mTitleList.set(0, mTitleList.get(1));
//            mTitleList.set(1, channel);

//        tabFragmentPageAdapter = new TabFragmentPageAdapter(getChildFragmentManager());
//        mVp.setAdapter(tabFragmentPageAdapter);
//        mVp.setOffscreenPageLimit(3);
//        mTlIndicator.setupWithViewPager(mVp);

            if (fragmentManager != null) {
                FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
                for (Fragment fragment1 : mFragmentList) {
                    fragmentTransaction.remove(fragment1);
                }
                fragmentTransaction.commit();
                fragmentManager.executePendingTransactions();
            }

            notifyDataSetChanged();
        }
        @NonNull
        @Override
        public Fragment getItem(int position) {
            return mFragmentList.get(position);
        }

        @Override
        public int getCount() {
            return mFragmentList.size();
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return mTitleList.get(position);
        }

    }

}
